package org.exez.modules.system.util;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.exez.modules.visitor.entity.VisitorAccessre;
import org.jeecgframework.poi.excel.annotation.Excel;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Base64;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 登录验证码工具类
 */
public class PingIpPortUtil {
    /**
     * ping ip
     *
     * @param ip
     * @return
     */
    public static boolean pingIp(String ip) {
        if (null == ip || 0 == ip.length()) {
            return false;
        }
        try {
            InetAddress.getByName(ip);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    /**
     * ping ip加端口
     *
     * @param ip
     * @param port
     * @return
     */
    public static boolean pingIpAndPort(String ip, String port) {
        if (null == ip || 0 == ip.length() || null == port || 0 == port.length() || !isInt(port) || !isRangeInt(port, 1024, 65535)) {
            return false;
        }
        return pingIpAndPort(ip, Integer.parseInt(port));
    }

    /**
     * ping ip加端口
     *
     * @param ip
     * @param port
     * @return
     */
    public static boolean pingIpAndPort(String ip, int port) {
        if (null == ip || 0 == ip.length() || port < 1024 || port > 65535) {
            return false;
        }
        if (!pingIp(ip)) {
            return false;
        }
        Socket s = new Socket();
        try {
            SocketAddress add = new InetSocketAddress(ip, port);
            s.connect(add, 3000);// 超时3秒
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            try {
                s.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * 判断是否是整数
     *
     * @param str
     * @return
     */
    public static boolean isInt(String str) {
        if (!isNumeric(str)) {
            return false;
        }
        // 该正则表达式可以匹配所有的数字 包括负数
        Pattern pattern = Pattern.compile("[0-9]+");

        Matcher isNum = pattern.matcher(str); // matcher是全匹配
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 匹配是否包含数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        if (null == str || 0 == str.length()) {
            return false;
        }
        if (str.endsWith(".")) {
            return false;
        }
        // 该正则表达式可以匹配所有的数字 包括负数
        Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");

        Matcher isNum = pattern.matcher(str); // matcher是全匹配
        if (!isNum.matches()) {
            return false;
        }

        return true;
    }

    /**
     * 是否在范围内
     *
     * @param str
     * @param start
     * @param end
     * @return
     */
    public static boolean isRangeInt(String str, int start, int end) {
        if (!isInt(str)) {
            return false;
        }
        int i = Integer.parseInt(str);
        return i > start && i < end;
    }

    public static void main(String[] args) {
        System.out.println(pingIpAndPort("183.134.255.19","8701"));

    }



}
